#include "drv_fan.h"
//PC6 高电平转	TIM3_CH1 和左前腿冲突
void drv_fan_init(void)
{
	GPIO_InitTypeDef  GPIO_InitStruct;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef TIM_OCInitStruct;
	
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
	GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6;
	GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOC,&GPIO_InitStruct);

	GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_TIM3);

	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period=10000-1;
	TIM_TimeBaseInitStruct.TIM_Prescaler=84-1;
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
 
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);

	TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;
	TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;
	TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;
	TIM_OCInitStruct.TIM_Pulse=0;
	TIM_OC1Init(TIM3,&TIM_OCInitStruct);
	
	TIM_ARRPreloadConfig(TIM3,ENABLE);
	TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
	TIM_CtrlPWMOutputs(TIM3,ENABLE);
	
	TIM_Cmd(TIM3,ENABLE);
}
void drv_fan_change(uint8_t fanstatus)//占空比30、50、70
{
	if(fanstatus==0)
	{
		drv_fan_close();
		return;
	}
	else if(fanstatus==1)
	{
		TIM_SetCompare1(TIM3,3000-1);
	}
	else if(fanstatus==2)
	{
		TIM_SetCompare1(TIM3,5000-1);		
	}
	else if(fanstatus==3)
	{
		TIM_SetCompare1(TIM3,8000-1);	
	}
}
void drv_fan_close(void)
{
	TIM_SetCompare1(TIM3,0);
}
